/*
 * @Author: Mr.Hope
 * @Date: 2019-06-20 18:38:04
 * @LastEditors: Mr.Hope
 * @LastEditTime: 2019-11-20 21:13:30
 * @Description: 导航插件
 */
import VueRouter, { Route } from 'vue-router';
import { VueConstructor } from 'vue';
import { Modal } from 'ant-design-vue';
const { resolve } = require('path');

export const navigate = (
  url: string | undefined,
  router: VueRouter,
  route: Route
) => {
  if (url)
    if (url && url[0] === '/')
      // Inner absolute path
      router.push(url);
    else if (url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1)
      // Outter url
      window.open(url);
    else {
      // Inner relative path
      const base = route.path.slice(0, route.path.lastIndexOf('/'));

      router.push(resolve(`${base}/`, url));
    }
  else
    Modal.confirm({
      title: '地址错误',
      content: '链接地址有误，请汇报给Mr.Hope!',
      autoFocusButton: 'cancel',
      cancelText: '确定',
      okText: '汇报',
      okType: 'danger',
      onOk: () => {
        router.back();
        window.open(
          'http://wpa.qq.com/msgrd?v=3&uin=1178522294&site=qq&menu=yes'
        );
      },
      onCancel: () => {
        router.back();
      }
    });
};

export default {
  install: (ctx: VueConstructor) => {
    ctx.prototype.$navigate = navigate;
  }
};
